package com.spx.kernel;

import java.util.Collection;
import java.util.Collections;
import java.util.Properties;
import java.util.Set;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.preferences.ConfigurationScope;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;

import com.spx.kernel.datasource.preferences.PreferenceConstants;
import com.spx.kernel.service.PersistService;

/**
 * This class controls all aspects of the application's execution
 */
public class Application implements IApplication
{

	static Collection<?> internalClipboard = Collections.EMPTY_LIST;

	private static final String CLASS = "class";
	private static final String MODEL_TYPES_EXTENSIONPOINT_ID = "com.spx.model.types";
	private static PersistService persistService;
	private static Properties properties = new Properties();

	/*
	 * (non-Javadoc)
	 * 
	 * @seeorg.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app.
	 * IApplicationContext)
	 */
	public Object start(IApplicationContext context) throws Exception
	{
		final Display display = PlatformUI.createDisplay();
		try
		{

			final int returnCode = PlatformUI.createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor());
			if (returnCode == PlatformUI.RETURN_RESTART)
			{
				return IApplication.EXIT_RESTART;
			} else
			{
				return IApplication.EXIT_OK;
			}
		} finally
		{
			display.dispose();
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.equinox.app.IApplication#stop()
	 */
	public void stop()
	{
		final IWorkbench workbench = PlatformUI.getWorkbench();
		if (workbench == null)
		{
			return;
		}
		final Display display = workbench.getDisplay();
		display.syncExec(new Runnable()
		{
			public void run()
			{
				if (!display.isDisposed())
				{
					workbench.close();
				}
			}
		});
	}

	public static void connect()
	{
		
		
		
		
	
        
        IEclipsePreferences preferences = new  ConfigurationScope().getNode( KernelActivator.PLUGIN_ID+"/"+PreferenceConstants.P_DATASOURCE_NODE_NAME);

       
		properties.put("hibernate.connection.driver_class", preferences.get(PreferenceConstants.P_DATASOURCE_DRIVER,"not-set"));
		properties.put("hibernate.connection.username", preferences.get(PreferenceConstants.P_DATASOURCE_USER,"not-set"));
		properties.put("hibernate.connection.password", preferences.get(PreferenceConstants.P_DATASOURCE_PASSWORD,"not-set"));
		properties.put("hibernate.connection.url", preferences.get(PreferenceConstants.P_DATASOURCE_URL,"not-set"));
		properties.put("hibernate.max_fetch_depth", "3");
		if (preferences.getBoolean(PreferenceConstants.P_DATASOURCE_REBUILD,false))
		{
			properties.put("hibernate.hbm2ddl.auto", "create");
		}

		persistService = com.spx.kernel.service.CoreServiceLocator.getInstance().getService("persist-service", PersistService.class);

		if (persistService != null)
		{

			persistService.setProperties(properties);

			buildTypeList();
		}

		try
		{

			persistService.start();
		} catch (final Exception e)
		{

			e.printStackTrace();
		}
	}

	private static void buildTypeList()
	{
		final IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(MODEL_TYPES_EXTENSIONPOINT_ID);
		final IExtension[] extensions = extensionPoint.getExtensions();
		for (final IExtension extension : extensions)
		{
			final IConfigurationElement[] configs = extension.getConfigurationElements();
			for (final IConfigurationElement config : configs)
			{
				try
				{
					final com.spx.kernel.types.IModelTypes modelTypeHolder = (com.spx.kernel.types.IModelTypes) config.createExecutableExtension(CLASS);
					final Set<Class<? extends com.spx.kernel.types.ModelType>> modelList = modelTypeHolder.getTypes();
					persistService.addTypes(modelList);
				} catch (final CoreException e)
				{
					e.printStackTrace();
				}
			}
		}

	}

	public static synchronized void setClipboard(Collection<?> objects)
	{
		internalClipboard = objects;
	}

	public static synchronized Collection<?> getClipboard()
	{
		return internalClipboard;
	}

}
